package com.nosweattechnet.server;

import java.util.ArrayList;
import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import android.os.AsyncTask;
import android.util.Log;

//The basic Asynchronous Task to communicate with the server
public class CommunicatorAsyncTask extends AsyncTask<Void,Void,JSONArray> {
	private Communicator comm;
	
	//Initializes the Communicator
	public CommunicatorAsyncTask(ArrayList<NameValuePair> nvp){
		if(nvp==null){
			comm=new Communicator();
		}else{
			comm=new Communicator(nvp);
		}
	}
	
	//Initializes the Communicator
	public CommunicatorAsyncTask(){
		comm=new Communicator();
	}
	
	//Allows subclasses to add pairs to the communicator
	protected void addNameValuePair(NameValuePair nvp){
		comm.addNameValuePair(nvp);
	}
	
	//Allows subclasses to add pairs to the communicator
	protected void addNameValuePair(ArrayList<NameValuePair> nvp){
		comm.addNameValuePair(nvp);
	}

	@Override
	//This function is called by the superclass.  It tells the Communicator to send the request.
	//The resulting JSONArray is then stored, and can be retrieved using the get() function.
	protected JSONArray doInBackground(Void... params) {
		JSONArray response=null;
		try {
			response=comm.sendRequest();
		} catch (JSONException e) {
			Log.e("log_tag","Error converting result: "+e.toString());
		}
		return response;
	}
}
